package com.ld.control.fileter;


import com.ld.control.business.user.service.UserAccountService;
import com.ld.control.sysenum.ResultResponseEnum;
import com.ld.control.utils.ResponseUtils;
import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.IOException;

@Component
@Slf4j
@WebFilter(urlPatterns = "/*", filterName = "tokenFilter1")
public class TokenFilter implements Filter {

    @Autowired
    private UserAccountService userAccountService;

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        //ResultResponseEnum resultResponseEnum = userAccountService.checkUserToken();
        //解决跨域的问题
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
        response.setHeader("Access-Control-Allow-Headers", "*");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Credentials", "false");
        /*if (!resultResponseEnum.getCode().equals(ResultResponseEnum.SUCCESS.getCode())) {
            ResponseUtils.returnErrorInfo(response, resultResponseEnum);
            return;
        }*/
        filterChain.doFilter(request, response);
    }
}
